package com.houdoudou.common;

import java.io.File;
import java.net.InetAddress;

public class CommonFileFunction {

	 public static String getPicBaseAddress(){
		 String result="";
		 
		 try {
		        String addr = InetAddress.getLocalHost().toString();
		        String ip=addr.substring(addr.lastIndexOf("/")+1);
		       // result = addr.getHostName();
		        String PicFolder="userImages";
		        result="http://"+ip+":8080/houdoudou/"+PicFolder+"/";
		        System.out.print("PicBaseAddress==="+result);
		        System.out.print("ip==="+ip);
		    } catch (Exception e) {
		    	e.printStackTrace();
		    }
		 return result;
	 } 
	 
	 public static void deletePic(String Pic_Dir){
		 
		 String file_to_delete ="";
		 //E:\apache-tomcat-7.0.59-windows-x64\apache-tomcat-7.0.59\bin
		 String localPath= System.getProperty("user.dir");
		 String[] localPathParse= localPath.split("\\\\");
		 for(int i=0;i<localPathParse.length;i++){
	      	  System.out.println("--"+i+"---"+localPathParse[i]); 
	         } 
		 file_to_delete=localPathParse[0]+"/"+localPathParse[1]+"/"+localPathParse[2]+"/"+"webapps"+"/"+"houdoudou";
		 //重写名称和目录的处理
        String[] nameparse=Pic_Dir.split("\\/");
        for(int i=0;i<nameparse.length;i++){
     	  System.out.println("--"+i+"---"+nameparse[i]); 
        } 
        
        //一级目录
        file_to_delete=file_to_delete+"/"+nameparse[4];
        File fUploadDir = new File(file_to_delete); 
        if(!fUploadDir.exists()) 
        {          
                 System.out.println("文件不存在"+file_to_delete);                          
         }  
      //二级目录
        file_to_delete=file_to_delete+"/"+nameparse[5];
        File fUploadDir2 = new File(file_to_delete); 
        if(!fUploadDir2.exists()) 
        {             
           	  System.out.println("文件不存在"+file_to_delete);                
         }  
        //三级目录
        file_to_delete=file_to_delete+"/"+nameparse[6];
        File fUploadDir3 = new File(file_to_delete); 
        if(!fUploadDir3.exists()) 
        {            
           	  System.out.println("文件不存在"+file_to_delete);                      
         }  
        // 名称
        file_to_delete=file_to_delete+"/"+nameparse[7];
        File file = new File(file_to_delete);  
        // 路径为文件且不为空则进行删除  
        if (file.isDirectory() && file.exists()) {  
           boolean resutlt= deleteDir(file);
      	  System.out.println("删除文件夹"+file_to_delete+"成功？"+resutlt); 
        }else{
       	  System.out.println("文件不存在"+file_to_delete); 
        }  
	 }
	 
	 private static boolean deleteDir(File dir) {
	        if (dir.isDirectory()) {
	            String[] children = dir.list();
              //递归删除目录中的子目录下
	            for (int i=0; i<children.length; i++) {
	                boolean success = deleteDir(new File(dir, children[i]));
	                if (!success) {
	                    return false;
	                }
	            }
	        }
	        // 目录此时为空，可以删除
	        return dir.delete();
	    }
}
